const signUpButton = document.getElementById('signUp');
const signInButton = document.getElementById('signIn');
const container = document.getElementById('container');


signUpButton.addEventListener('click', () => {
	container.classList.add("right-panel-active");
});


signInButton.addEventListener('click', () => {
	container.classList.remove("right-panel-active");
});
const loginServer = `${window.location.protocol}//${window.location.hostname}:8089/`;
// const loginServer = `http://192.168.168.4:8089/`;
//登录
function login() {
	let email = document.getElementById('loginemail').value;
	let pass = document.getElementById('loginpass').value;
	if(email == "" || pass == ""){
		alert("用户名和密码不能为空！");
		return false;
	}
	$.ajax({
		//请求方式
		type: "POST",
		//文件位置
		url: `${loginServer}user/login`,
		//返回数据格式为json,也可以是其他格式
		dataType: "json",
		data: {
			"username": email,
			"password": md5(pass,32)
		},
		//请求成功后要执行的函数
		success: function(data) {
			if (data.code == 200) {
				localStorage.setItem("userinfo", JSON.stringify(data));
				window.location.href = "index.html";
			}else if(data.code == 500){
				alert(data.msg);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.error(XMLHttpRequest.responseJSON.msg);
		}
	});
}
//注册
function reg() {
	let name = document.getElementById('regname').value;
	let email = document.getElementById('regemail').value;
	let pass = document.getElementById('regpass').value;
	$.ajax({
		type: "POST",
		url: `${loginServer}user/register`,
		dataType: "json",
		data: {
			"username": email,
			"password": md5(pass,32),
			"nickname": name
		},
		success: function(data) {
			alert(data.msg);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.error(XMLHttpRequest.responseJSON.msg);
		}
	});
}
let userinfo = JSON.parse(localStorage.getItem('userinfo'));
if (userinfo != null && userinfo.code == 200) {
	window.location.href = "index.html";
}